package cn.jinhaze.service.impl;

import cn.jinhaze.common.pojo.dto.DishPageDTO;
import cn.jinhaze.common.pojo.entity.Dish;
import cn.jinhaze.common.pojo.vo.DishVO;
import cn.jinhaze.mapper.DishMapper;
import cn.jinhaze.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: L29
 * @create: 2024-12-24 21:14
 * @Version 1.0
 */
@Service
public class DishServiceImpl implements DishService {

	@Autowired
	private DishMapper dishMapper;

	@Override
	public Dish getById(Long id) {
		return dishMapper.selectById(id);
	}

	@Override
	public List<DishVO> list(DishPageDTO dishPageDTO) {
		LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		if (dishPageDTO.getName() != null) {
			queryWrapper.like(Dish::getName, dishPageDTO.getName());
		}
		if (dishPageDTO.getMinPrice() != null) {
			queryWrapper.ge(Dish::getPrice, dishPageDTO.getMinPrice());
		}
		if (dishPageDTO.getMaxPrice() != null) {
			queryWrapper.le(Dish::getPrice, dishPageDTO.getMaxPrice());
		}
		if (dishPageDTO.getStatus() != null) {
			queryWrapper.eq(Dish::getStatus, dishPageDTO.getStatus());
		}


		List<Dish> dishes = dishMapper.selectList(queryWrapper);
		return dishes.stream().map(
				dish -> {
					DishVO dishVO = new DishVO();
					BeanUtils.copyProperties(dish, dishVO);
					return dishVO;
				}).toList();
	}

	@Override
	public void batchDelete(List<Long> ids) {
		dishMapper.deleteBatchIds(ids);
	}

	@Override
	public void addDish(DishVO dishVO) {
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishVO, dish);
		dishMapper.insert(dish);
	}

	@Override
	public void updateDish(DishVO dishVO) {
		Dish dish = new Dish();
		BeanUtils.copyProperties(dishVO, dish);
		dishMapper.updateById(dish);
	}
}